package com.tangsm.spring.boot.xml.controller;

import com.tangsm.spring.boot.xml.domain.UserInfo;
import com.tangsm.spring.boot.xml.domain.WxEventAuthInfo;
import com.tangsm.spring.boot.xml.domain.WxXmlEventRequest;
import com.tangsm.spring.boot.xml.domain.WxXmlEventResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 微信XML格式事件处理
 *
 * @author tangsm
 */
@RestController
@RequestMapping("/wx")
public class WxXmlEventController {
    private static final Logger log = LoggerFactory.getLogger(WxXmlEventController.class);
    /**
     * 微信XML格式事件接收接口
     *
     * @param msg     请求参数
     * @param request 请求对象
     * @return 响应结果
     */
    @PostMapping(path = "xml/event",
            consumes = {"application/xml", "text/xml"},
            produces = "application/xml;charset=utf-8")
    public WxXmlEventResponse xmlBack(@RequestBody WxXmlEventRequest msg, HttpServletRequest request) {
        WxXmlEventResponse res = new WxXmlEventResponse();
        res.setFromUserName(msg.getToUserName());
        res.setToUserName(msg.getFromUserName());
        res.setCreateTime(System.currentTimeMillis() / 1000);
        res.setMsgType("text");
        res.setContent("hello: " + LocalDateTime.now());
        return res;
    }

    /**
     * 微信XML格式事件接收接口
     *
     * @param msg     请求参数
     * @param userInfo 用户参数
     * @return 响应结果
     */
    @PostMapping(path = "xml2/event",
            consumes = {"application/xml", "text/xml"},
            produces = "application/xml;charset=utf-8")
    public WxXmlEventResponse xmlBack(@RequestBody WxXmlEventRequest msg, UserInfo userInfo) {
        log.info("请求参数：{}", msg.toString());
        log.info("用户参数：{}", userInfo.toString());
        WxXmlEventResponse res = new WxXmlEventResponse();
        res.setFromUserName(msg.getToUserName());
        res.setToUserName(msg.getFromUserName());
        res.setCreateTime(System.currentTimeMillis() / 1000);
        res.setMsgType("text");
        res.setContent("hello: " + LocalDateTime.now());
        return res;
    }

    /**
     * Json格式事件接收接口
     *
     * @param msg     请求参数
     * @param request 请求对象
     * @return 响应结果
     */
    @PostMapping(path = "json/event")
    public WxXmlEventResponse jsonBack(@RequestBody WxXmlEventRequest msg, HttpServletRequest request) {
        WxXmlEventResponse res = new WxXmlEventResponse();
        res.setFromUserName(msg.getToUserName());
        res.setToUserName(msg.getFromUserName());
        res.setCreateTime(System.currentTimeMillis() / 1000);
        res.setMsgType("text");
        res.setContent("hello: " + LocalDateTime.now());
        return res;
    }

    /**
     * Json格式事件接收接口（自动装载Url中的参数）
     *
     * @param msg      请求参数
     * @param userInfo 用户信息
     * @return 响应结果
     */
    @PostMapping(path = "json2/event")
    private WxXmlEventResponse jsonBack(@RequestBody WxXmlEventRequest msg, UserInfo userInfo) {
        WxXmlEventResponse res = new WxXmlEventResponse();
        res.setFromUserName(msg.getToUserName());
        res.setToUserName(msg.getFromUserName());
        res.setCreateTime(System.currentTimeMillis() / 1000);
        res.setMsgType("text");
        res.setContent("hello: " + LocalDateTime.now());
        return res;
    }

    /**
     * xml请求报文接收测试
     *
     * @param request 请求参数
     * @param wxEventAuthInfo 微信事件授权信息
     * @return 响应结果
     */
    @PostMapping(value = "/wxEvent",
            consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE},
            produces = MediaType.APPLICATION_XML_VALUE)
    public WxXmlEventResponse wxEvent(@RequestBody WxXmlEventRequest request, WxEventAuthInfo wxEventAuthInfo) {
        log.info("请求参数：{}", request.toString());
        log.info("微信事件授权信息：{}", wxEventAuthInfo.toString());
        WxXmlEventResponse response = new WxXmlEventResponse();
        response.setFromUserName(request.getToUserName());
        response.setToUserName(request.getFromUserName());
        response.setCreateTime(System.currentTimeMillis() / 1000);
        response.setMsgType("text");
        response.setContent("hello: " + LocalDateTime.now());
        return response;
    }
}
